/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import abstractmodel.IAccount;
import abstractmodel.IBook;
import abstractmodel.IDetail;
import abstractmodel.IInfo;
import abstractmodel.StatisticsType;
import java.util.ArrayList;

/**
 *
 * @author fightfxj
 */
public class Detail implements abstractmodel.IDetail{
    
    DetailInfo detailInfo;
    /**
     *Detail 所属的account ;
     */
    IAccount ownerAccount;
    
    public Detail() {
        detailInfo = new DetailInfo();
    }
    
    public Detail(IAccount account) {
        detailInfo = new DetailInfo();
        ownerAccount = account;
    }

    public IAccount getOwnerAccount() {
        return ownerAccount;
    }

    @Override
    public void setOwnerAccount(IAccount ownerAccount) {
        this.ownerAccount = ownerAccount;
    }



    @Override
    public IInfo getDetailInfo() {
        return detailInfo;
    }

    @Override
    public boolean setDetailInfo(IInfo info) {
        detailInfo = (DetailInfo) info;
        // 通过帐户设置当前明细的余额
        //((BankAccount)account).printAllAccountInfo();
        //detailInfo.setValue("余额", this.ownerAccount.getTotalLeftMoney() + detailInfo.getMoneyAmount());
        if(info.getValue("余额").getString().equals(""))
            detailInfo.setValue("余额", detailInfo.getMoneyAmount());
        // TODO 缺可用额度的处理
        // this.getOwnerAccount();
        /**
         * 写入数据库
         */
        ModelFactory.data.saveDetailInfo(detailInfo);
        return true;
    }
    
    /**
     * 打印明细的所有相关信息，供调试用
     */
    public void printAllDetailInfo() {
        //{"收支日期", "原因", "数额", "余额", "备注"}
        System.out.println("Titles : ");
        for (int i = 0;i < detailInfo.getTitles().length; i ++)
            System.out.print(detailInfo.getTitles()[i] +  " ");
        System.out.println();        
        System.out.println("收支日期：" + detailInfo.getValue("收支日期").getString());
        System.out.println("余额：" + detailInfo.getValue("余额").getDouble());
        System.out.println("数额：" + detailInfo.getValue("数额").getDouble());
        System.out.println("原因：" + detailInfo.getValue("原因").getString());
        System.out.println("备注：" + detailInfo.getValue("备注").getString());
        System.out.println("ID：" + detailInfo.getValue("id").getString());
        System.out.println("parentID：" + detailInfo.getValue("parentID").getString());
    }

}
